
    //var prefs = new gadgets.Prefs();
        //setValue("yourSkill", prefs.getInt("skill"));
    //setValue("yourStam", prefs.getInt("stamina"));
    //setValue("yourLuck", prefs.getInt("luck"));
    //setValue("yourGold", prefs.getInt("gold"));
  function roll (dice) {
    if (dice.toUpperCase().indexOf("D") == 0) {
      dice = 1 + dice;
    }
    var d = dice.toUpperCase().split("D");
    var result = 0;
    for (i=0;i<d[0];i++) {
      result += Math.floor(Math.random()*d[1])+1;
    }
    return result;
  }

  function rollD6() {
     output("<h3>You rolled </h3><span class=\"num\">" + roll('D6') + "</span>");
  }

    function getValue(valueID) {
      var value = parseInt(document.getElementById(valueID).value);
      if (isNaN(value)) {
        value = 0;
        setValue(valueID, 0);
      }
      return value;
      // should throw error if valueID does not exist
    }

    function setValue(valueID, value) {
      document.getElementById(valueID).value = value;
    }

    function attackRound() {
      var yourStam = getValue('yourStam');
      var oppStam = getValue('oppStam');
      var yourRoll = roll('2D6');
      var oppRoll = roll('2D6');
      var yourSkill = getValue('yourSkill');
      var oppSkill = getValue('oppSkill');
      var yourTotal = yourSkill + yourRoll;
      var oppTotal = oppSkill + oppRoll;
      var result = "";
      if (yourTotal > oppTotal) {
        result = " <b>You hit</b> your opponent for 2 points of damage.";
        setValue('oppStam', getValue('oppStam')-2);
      } else {
        result = " Your opponent <b>hits you</b> for 2 points of damage.";
        setValue('yourStam', getValue('yourStam')-2);
      }
      
      // Show dice rolls
      result += " <br/><small>(you: " + yourSkill + " + " + yourRoll + " = " + yourTotal + " vs. opp: " + oppSkill + " + " + oppRoll + " = " + oppTotal + ")</small><br/>";

      // If either combatant is dead, say so.
      if (getValue('yourStam') <= 0) {
        result += "<h3>You are dead</h3>";
      } else if (getValue('oppStam') <= 0){
        result += "<h3>Your opponent is dead</h3>";
      } 
                        output(result);
      return result;
    }

    function attack() {
      var yourStam = getValue('yourStam');
      var startStam = yourStam;
      var oppStam = getValue('oppStam');
      var result = "";
      var round = 1;
      while(!((yourStam <= 0) || (oppStam <= 0))) {
        result = result + "<br/>" + round + ": " + attackRound();
        yourStam = getValue('yourStam');
        oppStam = getValue('oppStam');
        round += 1;
      }
      if (yourStam <= 0) {
        result = "<h3>You died</h3> " + result;
      } else {
        result = "<h3>You defeated your opponent. You lost " + (startStam-getValue('yourStam')) + " Stamina.</h3> "  + result;
      } 
      output(result);
    }

    function testLuck() {
      var yourLuck = getValue('yourLuck');
      var yourRoll = roll('2D6');
      var result = "";

      if (yourRoll <= yourLuck) {
        result = "LUCKY";
      } else {
        result = "UNLUCKY";
      }
      setValue('yourLuck', yourLuck-1);
      output("You rolled " + yourRoll + "<h3>You are " + result + "</h3>");
    }

  function eatProvision() {
    setValue('yourStam', getValue('yourStam')+4);
  }

  function rollCharacter() {
    var yourSkill = roll('D6')+6;
    var yourStam = roll('2D6')+12;
    var yourLuck = roll('D6')+6;

    setValue('yourStam', yourStam);
    setValue('yourSkill', yourSkill);
    setValue('yourLuck', yourLuck);
    setValue('yourGold', 20);
    setInitials(yourSkill, yourStam, yourLuck);
  }

  function setInitials(skill, stamina, luck) {
    document.getElementById('initialSkill').innerHTML="("+skill+")";
    document.getElementById('initialStam').innerHTML="("+stamina+")";
    document.getElementById('initialLuck').innerHTML="("+luck+")";
  }

  function output(content) {
    document.getElementById('output').innerHTML=content;
  }

    function saveStats() {
      //prefs.set("skill", getValue("yourSkill"));
      //prefs.set("stamina", getValue("yourStam"));
      //prefs.set("luck", getValue("yourLuck"));
      //prefs.set("gold", getValue("yourGold"));
    }

